import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { svgTagView } from './src/commons/svg'
import path from 'path'

// 获取当前时间戳
const timeStamp = new Date().getTime()
const devDepends = require('./package.json').dependencies

export default defineConfig({
  optimizeDeps: {
    include: ['vue']
  },
  plugins: [
    vue(),
    svgTagView('./src/assets/svg/')
  ],
  base: './',
  // 路径别名
  resolve: {
    alias: {
      '@': path.resolve('src')
    },
    extensions: ['.css', '.js', '.json', '.vue']
  },
  // 开发时候的端口号，默认为3000
  server: {
    host: '0.0.0.0',
    port: 8011
  },
  // 打包配置
  build: {
    // 打包文件夹名称
    outDir: 'admin',
    // 开启压缩
    minify: true,
    compress: {
      drop_console: true,
      drop_debugger: true
    },
    // 打包后的文件名
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${timeStamp}.js`,
        chunkFileNames: `assets/[name].${timeStamp}.js`,
        assetFileNames: `assets/[name].${timeStamp}.[ext]`,
        // 根据不同的js库 拆分包，减少index.js包体积
        manualChunks(id) {
          if (id.includes('node_modules')) {
            // 指定需要拆分的第三方库或菜单
            const dependenciesKeys = Object.keys(devDepends)
            const match = dependenciesKeys.find((item) => {
              return id.includes(item)
            })
            const needSplit = ['@wangeditor-next/editor']
            if (match && needSplit.includes(match)) {
              return match
            }
          }
        }
      }
    }
  }
})
